﻿var express = require('express');
var router = express.Router();	
var dbTodo = require('../dao/todoDao');
var dbCategory = require('../dao/categoryDao');
var dbTag = require('../dao/tagDao');
var site_type =	config = require("../config").site_type;
var dateUtils = require('../modules/dateutils');

var index = function(req, res){
	var id = req.params.id;
}

/*
req.params.id  route url 的参数 url/:id
req.body.id    post 参数   id
req.query.id   get  参数   id
req.param("id")     post/get  参数   id
*/
exports.getList = function (req, res, next) {
	var type = req.params.type || site_type.todo;
	var db = dbTodo;
	if(type == site_type.category){
		db = dbCategory;
	}
	else if(type == site_type.tag){
		db = dbTag;
	}

    var curDate = req.param("date") || "";
    curDate = curDate==""?new Date():new Date(curDate);
    var curWeekIndex = dateUtils.GetWeekIndex(curDate);
    var weekStartDate = dateUtils.getBeginDateOfWeek1(curDate.getFullYear(), curWeekIndex);
    var weekEndDate = dateUtils.getEndDateOfWeek1(curDate.getFullYear(), curWeekIndex);

    var condition = {
        keyword: req.param("keyword") || '',
        title: req.param("title") || '',
        content: req.param("content") || '',
        startDate: req.param("startDate") ||  weekStartDate,
        endDate: req.param("endDate") || weekEndDate,
        category: req.param("category") || "",
        tag: req.param("tag") || "",

        class_name: req.param("className") || '',
        color: req.param("color") || ''
    };

	db.search(condition, function (err, docs, next) {
        if (err) {
            return next(err);
        }
		else {
			res.render('task_list.ejs', {
				siteType: site_type,
				curType: type,
				todos: docs
			});
		}
    });	
};